package client;

import idl.client.ClientInterface;
import idl.client.ClientInterfaceHelper;
import idl.client.ServeurInterface;
import idl.client.ServeurInterfaceHelper;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import client.ui.Home;

public class Client {
	public static void main(String[] args) {

		try {
			// create and initialize the ORB
			ORB orb = ORB.init(args, null);

			// get the root naming context
			org.omg.CORBA.Object objRef = orb
					.resolve_initial_references("NameService");
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

			// Instantiate Servant and create reference
			POA rootPOA = POAHelper.narrow(orb
					.resolve_initial_references("RootPOA"));
			ClientImpl clientServicesImpl = new ClientImpl();
			rootPOA.activate_object(clientServicesImpl);
			ClientInterface clientServices = ClientInterfaceHelper
					.narrow(rootPOA.servant_to_reference(clientServicesImpl));

			// resolve the Object Reference in Naming
			NameComponent nc = new NameComponent("Neytiri", "");
			NameComponent path[] = { nc };

			ServeurInterface serveur = ServeurInterfaceHelper.narrow(ncRef
					.resolve(path));

			// Activate rootpoa
			rootPOA.the_POAManager().activate();

			if (serveur == null || clientServices == null) {
				throw new Exception(
						"Impossible de trouver le client ou le serveur");
			}
			
			// Interface
			new Home(serveur, clientServices, clientServicesImpl);

			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("ERROR : " + e);
		}
	}
}
